# 现在接收 pkgs 作为参数（来自官方 Nixpkgs + 自定义 overlay）
{ pkgs }: let
  # 导入 QQ 的源码配置（同目录下的 sources.nix）
  sources = import ./sources.nix { inherit (pkgs) fetchurl; };
  source = sources.${pkgs.stdenv.hostPlatform.system}
    or (throw "Unsupported system: ${pkgs.stdenv.hostPlatform.system}");
  pname = "qq";
  inherit (source) version src;
  passthru = {
    updateScript = ./update.sh;
  };
  meta = {
    homepage = "https://im.qq.com/index/";
    description = "Messaging app (personal Nixpkg)";
    platforms = [
      # "aarch64-darwin"
      "aarch64-linux"
      # "x86_64-darwin"
      "x86_64-linux"
    ];
    license = pkgs.lib.licenses.unfree;
    sourceProvenance = with pkgs.lib.sourceTypes; [ binaryNativeCode ];
    maintainers = [ "binigo" ];  # 改为自己的名字
  };
in
pkgs.stdenv.mkDerivation {
  inherit pname version src passthru meta;
  # 依赖从 pkgs 中获取（之前是函数参数，现在统一用 pkgs.xxx）
  nativeBuildInputs = [
    pkgs.autoPatchelfHook
    pkgs.makeShellWrapper
    pkgs.wrapGAppsHook3
    pkgs.dpkg
  ];
  buildInputs = [
    pkgs.alsa-lib
    pkgs.at-spi2-core
    pkgs.cups
    pkgs.glib
    pkgs.gtk3
    pkgs.libdrm
    pkgs.libpulseaudio
    pkgs.libgcrypt
    pkgs.libkrb5
    pkgs.libgbm
    pkgs.nss
    pkgs.vips
    pkgs.xorg.libXdamage
    pkgs.xorg.libX11
    pkgs.xorg.libxcb
  ];
  dontWrapGApps = true; # 手动处理 GTK 依赖
  runtimeDependencies = map pkgs.lib.getLib [ # 手动添加 GTK 依赖
    pkgs.systemd
    pkgs.libkrb5
  ];
  installPhase = ''
    # 你的原有安装脚本（不变，只是依赖都改为 pkgs.xxx）
    runHook preInstall
    mkdir -p $out/bin
    cp -r opt $out/opt
    cp -r usr/share $out/share
    substituteInPlace $out/share/applications/qq.desktop \
      --replace-fail "/opt/QQ/qq" "$out/bin/qq" \
      --replace-fail "/usr/share" "$out/share"
    makeShellWrapper $out/opt/QQ/qq $out/bin/qq \
      --prefix XDG_DATA_DIRS : "$GSETTINGS_SCHEMAS_PATH" \
      --prefix LD_PRELOAD : "${pkgs.lib.makeLibraryPath [ pkgs.libssh2 ]}/libssh2.so.1" \
      --prefix LD_LIBRARY_PATH : "${
        pkgs.lib.makeLibraryPath [
          pkgs.libGL
          pkgs.libuuid
        ]
      }" \
      --add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --disable-gpu --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}" \
      --add-flags ${pkgs.lib.escapeShellArg ""} \
      "''${gappsWrapperArgs[@]}"
    rm -r $out/opt/QQ/resources/app/sharp-lib
    rm -r $out/opt/QQ/resources/app/libssh2.so.1
    ln -s ${pkgs.libayatana-appindicator}/lib/libayatana-appindicator3.so \
      $out/opt/QQ/libappindicator3.so
    ln -s ${pkgs.libnotify}/lib/libnotify.so \
      $out/opt/QQ/libnotify.so
    runHook postInstall
  '';
}
